package com.yaowk.platform.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yaowk.authc.model.User;
import com.yaowk.common.kit.ResultKit;
import com.yaowk.common.validator.RequiredIdValidator;
import com.yaowk.platform.model.Trade;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

/**
 * 行业
 *
 * @authc yaowk
 * 2017/4/30
 */
public class TradeController extends Controller {

    /**
     * 行业列表
     *
     * @param id|平台id|Integer|必填
     * @title 行业列表
     */
    @Before(RequiredIdValidator.class)
    @RequiresPermissions("platform:read")
    public void list() {
        Integer platformId = getParaToInt("id");
        List<Trade> tradeList = Trade.dao.findByPlatformId(platformId);
        renderJson(ResultKit.success(tradeList));
    }

    /**
     * 添加行业
     *
     * @param platform_id|平台id|Integer|必填
     * @param name|行业简称|String|否
     * @param full_name|行业全称|String|否
     * @param sort|排序|Integer|否
     * @title 添加行业
     */
    @RequiresPermissions("platform:update")
    public void add() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Trade trade = getModel(Trade.class, "");
        trade.setUserId(user.getId()).save();
        renderJson(ResultKit.success(this));
    }

    /**
     * 编辑行业
     *
     * @param id|id|Integer|必填
     * @param name|行业简称|String|否
     * @param full_name|行业全称|String|否
     * @param sort|排序|Integer|否
     * @title 编辑行业
     */
    @RequiresPermissions("platform:update")
    @Before(RequiredIdValidator.class)
    public void edit() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Trade trade = getModel(Trade.class, "");
        Trade old = Trade.dao.findById(trade.getId());
        old.setName(trade.getName())
                .setUserId(user.getId())
                .setFullName(trade.getFullName())
                .setSort(trade.getSort())
                .update();
        renderJson(ResultKit.success(this));
    }

    /**
     * 删除行业
     *
     * @param id|id|Integer|必填
     * @title 删除行业
     */
    @RequiresPermissions("platform:update")
    @Before(RequiredIdValidator.class)
    public void remove() {
        Integer id = getParaToInt("id");
        Trade.dao.findById(id).setStatus(0).update();
        renderJson(ResultKit.success(this));
    }
}
